﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static NikeSale.Bll.DBSelect;

namespace NikeSale.Ull
{
    public partial class RetGoodsForm : Form
    {
        double quata = 0;
        double refAmount = 0;
        /// <summary>
        /// 销售金额
        /// </summary>
        private double Quota {
            get{return quata;}
            set
            {
                quata = value;
                this.lb_交易Amount.Text = "交易金额:¥" + value.ToString("#0.00");
            }
        }
        /// <summary>
        /// 退款金额
        /// </summary>
        private double RefAmount
        {
            get { return refAmount; }
            set
            {
                this.refAmount = value;
                this.lb_退款Amount.Text = "退款金额:¥" + value.ToString("#0.00");
            }
        }

        public RetGoodsForm()
        {
            InitializeComponent();
        }

        private void SkinTxt_NumberCheck_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == (int)Keys.Back) return;
            if (char.IsNumber(e.KeyChar)) return;
            e.Handled = true;
        }

        private void dgv_Query列表_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dgv_Query列表.Rows.Count <= 0)
            {
                return;
            }
            this.StatisticsRefPrice();
        }

        private void bt_Query_Click(object sender, EventArgs e)
        {
            if (this.tb_流水号.Text.Length!=14)
            {
                Methods.ShowPrompt("流水号格式错误", 1000);
                return;
            }
            if (!ReturnGoods.HasReceiptsCode(this.tb_流水号.Text)) 
            {
                Methods.ShowPrompt("流水号不存在", 1000);
                return;
            }
            ClearRows();
            this.dgv_Query列表.DataSource = ReturnGoods.QueryData(this.tb_流水号.Text);
            StatisticsSumPrice();
            this.tb_流水号.Enabled = false;
            this.bt_Query.Enabled = false;
        }

        private void bt_退货_Click(object sender, EventArgs e)
        {
            if (this.dgv_Query列表.Rows.Count==0)
            {
                Methods.ShowPrompt("请先选择销售记录", 1500);
                return;
            }
            if (this.refAmount==this.Quota)
            {
                ReturnGoods.ClearSales(this.tb_流水号.Text);
                Cancel();
                Methods.ShowPrompt(" 操作完成", 1500);
                return;
            }
            for (int i = 0; i < this.dgv_Query列表.Rows.Count; i++)
            {
                if (this.dgv_Query列表.Rows[i].Selected)
                {
                    string sdid = this.dgv_Query列表.Rows[i].Cells["cm_SalesDetails"].Value.ToString();
                    ReturnGoods.DeleteSalesDetail(sdid, this.tb_流水号.Text);
                }
            }
            ReturnGoods.UpdateSales(this.RefAmount.ToString(), this.tb_流水号.Text);
            ClearRows();
            this.dgv_Query列表.DataSource = ReturnGoods.QueryData(this.tb_流水号.Text);
            StatisticsSumPrice();
            Cancel();
            Methods.ShowPrompt(" 操作完成", 1500);
        }

        private void bt_取消_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void Cancel()
        {
            ClearRows();
            this.Quota = 0;
            this.RefAmount = 0;
            this.tb_流水号.Enabled = true;
            this.bt_Query.Enabled = true;
        }

        /// <summary>
        /// 计算总Amount
        /// </summary>
        private void StatisticsSumPrice()
        {
            if (this.dgv_Query列表.Rows.Count<=0)
            {
                return;
            }
            foreach (DataGridViewRow row in this.dgv_Query列表.Rows)
            {
                double num = double.Parse(row.Cells["cm_Num"].Value.ToString());
                double price = double.Parse(row.Cells["cm_PaidPrice"].Value.ToString());
                this.Quota += (num * price);
            }
        }

        /// <summary>
        /// 计算退货金额
        /// </summary>
        private void StatisticsRefPrice()
        {
            double sum = 0;
            for (int i = 0; i < this.dgv_Query列表.Rows.Count; i++)
            {
                if (this.dgv_Query列表.Rows[i].Selected)
                {
                    double count = double.Parse(this.dgv_Query列表.Rows[i].Cells["cm_Num"].Value.ToString());
                    double price = double.Parse(this.dgv_Query列表.Rows[i].Cells["cm_PaidPrice"].Value.ToString());
                    sum += count * price;
                }
            }
            this.RefAmount = sum;
        }

        /// <summary>
        /// 清除当前数据源所有行
        /// </summary>
        private void ClearRows()
        {
            if (this.dgv_Query列表.DataSource==null)
            {
                return;
            }
            DataTable dt = this.dgv_Query列表.DataSource as DataTable;
            dt.Rows.Clear();
            this.dgv_Query列表.DataSource = dt;
        }
    }
}
